import Vue from 'vue'
import VueRouter from 'vue-router'
// import store from '../store/index.js' 这个vuex刷新丢失的问题,会使得登录后刷新还是跳到登录页

// import PersonalRecom from '@/views/findMusic/PersonalRecomView.vue'
// import LoginViews from '@/views/LoginViews.vue'
// import HomePage from '@/views/homePageView.vue'
// import FindMusicHome from '@/views/findMusic/findMusicHomeViews.vue'
// import Video from '@/views/video/videoViews.vue'
// import PlayList from '@/views/findMusic/playListViews.vue'
// import DailyRecom from '@/views/myMusic/dailyRecomViews.vue'
// import MySub from '@/views/myMusic/mySubViews.vue'
// import HistoryPlay from '@/views/myMusic/historyPlayViews.vue'
// import PlayListDetail from '@/views/playList/playListDetailViews.vue'
// import Singer from '@/views/findMusic/singerViews.vue'
// import SingerDetail from '@/views/singer/singerDetailViews.vue'
// import Top from '@/views/findMusic/topViews.vue'
// import UserDetail from '@/views/myMusic/userDetailViews.vue'
// import VideoContainer from '@/views/video/videoContainerViews.vue'
// import Mv from '@/views/video/mvViews.vue'
// import SearchList from '@/views/search/searchListViews.vue'
// import VideoDetail from '@/views/video/videoDetailViews.vue'

// 路由懒加载
// const  = () => import(/* web[ackChunkName: "" */ '')

const LoginViews = () => import(/* web[ackchunkname: "LoginViews" */ '@/views/LoginViews.vue')
const HomePage = () => import(/* web[ackchunkname: "LoginViews" */ '@/views/homePageView.vue')
const FindMusicHome = () => import(/* web[ackchunkname: "findMusicHomeViews" */ '@/views/findMusic/findMusicHomeViews.vue')
const Video = () => import(/* web[ackchunkname: "videoViews" */ '@/views/video/videoViews.vue')
const PersonalRecom = () => import(/* web[ackchunkname: "PersonalRecomView" */ '@/views/findMusic/PersonalRecomView.vue')
const PlayList = () => import(/* web[ackchunkname: "playListViews" */ '@/views/findMusic/playListViews.vue')
const DailyRecom = () => import(/* web[ackchunkname: "dailyRecomViews" */ '@/views/myMusic/dailyRecomViews.vue')
const MySub = () => import(/* web[ackchunkname: "mySubViews" */ '@/views/myMusic/mySubViews.vue')
const HistoryPlay = () => import(/* web[ackchunkname: "historyPlayViews" */ '@/views/myMusic/historyPlayViews.vue')
const PlayListDetail = () => import(/* web[ackchunkname: "playListDetailViews" */ '@/views/playList/playListDetailViews.vue')
const Singer = () => import(/* web[ackchunkname: "singerViews" */ '@/views/findMusic/singerViews.vue')
const SingerDetail = () => import(/* web[ackchunkname: "singerDetailViews" */ '@/views/singer/singerDetailViews.vue')
const Top = () => import(/* web[ackchunkname: "topViews" */ '@/views/findMusic/topViews.vue')
const UserDetail = () => import(/* web[ackchunkname: "userDetailViews" */ '@/views/myMusic/userDetailViews.vue')
const VideoContainer = () => import(/* web[ackchunkname: "videoContainerViews" */ '@/views/video/videoContainerViews.vue')
const Mv = () => import(/* web[ackchunkname: "mvViews" */ '@/views/video/mvViews.vue')
const SearchList = () => import(/* web[ackchunkname: "searchListViews" */ '@/views/search/searchListViews.vue')
const VideoDetail = () => import(/* web[ackchunkname: "videoDetailViews" */ '@/views/video/videoDetailViews.vue')

// 解决Vue-Router升级导致的Uncaught(in promise) navigation guard问题
// push
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push (location, onResolve, onReject) {
  if (onResolve || onReject) return originalPush.call(this, location, onResolve, onReject)
  return originalPush.call(this, location).catch(err => err)
}

// replace
const originalReplace = VueRouter.prototype.replace
VueRouter.prototype.replace = function replace (location) {
  return originalReplace.call(this, location).catch(err => err)
}
Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/home'
  },
  // 登录页
  {
    path: '/login',
    component: LoginViews,
    meta: { doNotNeedLogin: true }
  },
  {
    path: '/home',
    component: HomePage,
    redirect: '/findMusicHome',
    children: [
      { // 发现音乐
        path: '/findMusicHome',
        component: FindMusicHome,
        redirect: '/personalRecom',
        children: [
          // 个性推荐
          { path: '/personalRecom', component: PersonalRecom, meta: { doNotNeedLogin: true } },
          // 歌单
          { path: '/playList', component: PlayList, meta: { doNotNeedLogin: false } },
          // 歌手
          { path: '/artistlist', component: Singer, meta: { doNotNeedLogin: false } },
          // 排行榜
          { path: '/top', component: Top, meta: { doNotNeedLogin: false } },
          // 用户页
          { path: '/userDetail/:id', component: UserDetail, meta: { doNotNeedLogin: false } }
        ]
      },
      // 视频
      {
        path: '/videoMenu',
        component: VideoContainer,
        redirect: '/video',
        children: [
          { path: '/video', component: Video, meta: { doNotNeedLogin: false } },
          { path: '/mv', component: Mv, meta: { doNotNeedLogin: false } }
        ]
      },
      { path: '/videoDetail/:id', component: VideoDetail, props: true, meta: { doNotNeedLogin: false } },
      // 每日推荐
      { path: '/dailyRecom', component: DailyRecom, meta: { doNotNeedLogin: false } },
      // 最近播放
      { path: '/historyPlay', component: HistoryPlay, meta: { doNotNeedLogin: true } },
      // 我的收藏
      { path: '/subscribe', component: MySub, meta: { doNotNeedLogin: false } },
      // 歌单详情页
      { path: '/playListDetail/:id', component: PlayListDetail, meta: { doNotNeedLogin: false } },
      // 歌手详情
      { path: '/artistDetail/:id', component: SingerDetail, meta: { doNotNeedLogin: false } },
      { path: '/search/:keyword', component: SearchList, props: true, meta: { doNotNeedLogin: true, title: '搜索结果' } }// 搜索展示页

    ]
  }
]

const router = new VueRouter({
  routes
})

// router.beforeEach((to, from, next) => {
//   let isLogin = false
//   if (to.meta.doNotNeedLogin === false) {
//     // 要登录
//     isLogin = JSON.parse(window.sessionStorage.getItem('store')).isLogin ? JSON.parse(window.sessionStorage.getItem('store')).isLogin : false
//     if (isLogin === true) {
//       // 登录了
//       next()
//     } else {
//       // 没登录
//       Vue.prototype.$message.error('需要登录')
//       next('/login')
//     }
//   } else {
//     next()
//   }
// })
export default router
